#include<bits/stdc++.h>
#define int long long 
using namespace std;
int read(){
	int x=0,f=1;
	char ch=getchar();
	while(!isdigit(ch)){
		if(ch=='-')f=-1;
		ch=getchar();
	}
	while(isdigit(ch)){
		x=(x<<3)+(x<<1)+ch-'0';
		ch=getchar();
	}
	return x*f;
}
int _read(){
	char ch=getchar();
	while(!isdigit(ch))ch=getchar();
	return ch-'0';	
}
void write(int x){
	if(x<0)putchar('-'),x=-x;
	if(x>=10)write(x/10);
	putchar(x%10+'0');	
}
const int N=55,mod=1e9+7;
inline int Mod(int x){return x>=mod?x-mod:x;}
inline int poww(int x,int y){
	int sum=1;
	while(y){
		if(y&1)sum=1ll*x*sum%mod;
		x=1ll*x*x%mod;
		y>>=1;	
	}
	return sum;
}
int fac[N],inv[N];
void Init(int x){
	for(int i=fac[0]=1;i<=x;i++)fac[i]=1ll*i*fac[i-1]%mod;
	inv[x]=poww(fac[x],mod-2);for(int i=x;i;i--)inv[i-1]=1ll*i*inv[i]%mod;
}
inline void mul(int *a,int len,int maxn){
	for(int i=maxn;i>=0;i--){
		int sum=0;
		for(int j=0;j<=len&&j<=i;j++)sum=Mod(sum+1ll*a[i-j]*inv[j]%mod);
		a[i]=sum;
	}
	return;
}
int n,ans;
char s[N];
int a[N];
int b[N],c[N];
int tmp[N]={0,45,570,5955,57087,533943,5092230,50327850,508658835,141577340,346748073,63605721,691146974,747499077,538394750,265137024,856024632,427621971,516162438,440472972,712880662,791425298,982743029,438780983,383079911,230599376,905706960,197714458,341824438,954125005,98726828,948875929,829350644,341637655,685873588,261300047,133758564,474399230,421324133,863646999,790696542,382317337,207636000,134734713,4883420,322312711,935473927,97027071,538107255,825526842,189695037};
void sol(){
	ans=0;
	cin>>s+1;n=strlen(s+1);
	for(int i=1;i<=n;i++)a[i]=s[i]-'0';
	for(int i=1;i<n;i++)ans=Mod(ans+tmp[i]);
	for(int i=0;i<=9;i++){
		for(int j=1;j<=n;j++){
			for(int k=0;k<=i;k++)b[k]=j;
			for(int k=i+1;k<=9;k++)b[k]=j-1;
			for(int _=1;_<=n;_++){
				for(int k=0;k<a[_];k++){
					if(_==1&&k==0)continue;
					if(b[k]==0)continue;
					b[k]--;
					if(n-_>=b[i]){
						memset(c,0,sizeof c);c[0]=1;
						for(int l=0;l<=9;l++)if(l!=i)mul(c,b[l],n-_-b[i]);
						ans=Mod(ans+1ll*i*fac[n-_]%mod*c[n-_-b[i]]%mod*inv[b[i]]%mod);
					}
					b[k]++;
				}
				if(b[a[_]]==0)break;
				b[a[_]]--;
			}
		}
	}
	for(int i=0;i<=9;i++)b[i]=0;
	for(int i=1;i<=n;i++)b[a[i]]++;
	int maxn=9;
	for(int i=8;i>=0;i--)if(b[i]>b[maxn])maxn=i;
	ans=Mod(ans+maxn);
	write(ans),putchar('\n');
}
signed main(){
	freopen("give.in","r",stdin);
	freopen("give.out","w",stdout);
	Init(50);
	int T=read();
	while(T--)sol();
	return 0;
}
